package com.rongxue.utils;


import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class ExceptionUtils {

	//邮件服务器地址
	private static final String MAIL_SERVER = "smtp.qq.com";
	//登录邮箱
	private static final String LOGIN_ACCOUNT = "ghq1@qq.com";
	//授权码
	private static final String LOGIN_AUTH_CODE = "oopsujnrgqaibhcd";
	//发送人
	private static final String SENDER = "ghq1@qq.com";
	//收件人
	private static final String[] RECIPIENTS = {"457167074@qq.com"};
	//字符编码
	private static final String EMAIL_CONTENT_TYPE = "text/html;charset=utf-8";

	private static final Logger LOG = LoggerFactory.getLogger(ExceptionUtils.class);

	public static void recordException(Boolean flag, String emailSubject, String emailContent, Exception e) {

		StringBuffer stringBuffer = null;
		if (e != null){
			stringBuffer = new StringBuffer(e.toString() + "\n");
			StackTraceElement[] messages = e.getStackTrace();
			int length = messages.length;
			for (int i = 0; i < length; i++) {
				stringBuffer.append("\t"+messages[i].toString()+"\n");
			}

			emailContent += "\n"+stringBuffer;
		}

		LOG.error(emailSubject+emailContent);

		if (flag) {
			try {
				//跟smtp服务器建立一个连接
				Properties p = new Properties();
				//设置邮件服务器主机名
				p.setProperty("mail.host", MAIL_SERVER);
				//发送服务器需要身份验证,要采用指定用户名密码的方式去认证
				p.setProperty("mail.smtp.auth", "true");
				//发送邮件协议名称
				p.setProperty("mail.transport.protocol", "smtp");

				//开启SSL加密，否则会失败
				MailSSLSocketFactory sf = new MailSSLSocketFactory();
				sf.setTrustAllHosts(true);
				p.put("mail.smtp.ssl.enable", "true");
				p.put("mail.smtp.ssl.socketFactory", sf);

				// 创建session
				Session session = Session.getDefaultInstance(p, new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						//用户名可以用QQ账号也可以用邮箱的别名:第一个参数为邮箱账号,第二个为授权码
						PasswordAuthentication pa = new PasswordAuthentication(LOGIN_ACCOUNT, LOGIN_AUTH_CODE);
						return pa;
					}
				});


				//可以发送几封邮件:可以在这里 for循环多次
				//声明一个Message对象(代表一封邮件),从session中创建
				MimeMessage msg = new MimeMessage(session);
				//邮件信息封装
				//1发件人
				msg.setFrom(new InternetAddress(SENDER));

				//2收件人:可以多个
				//一个的收件人
				//msg.setRecipient(RecipientType.TO, new InternetAddress("linsenzhong@126.com"));

				InternetAddress[] receptientsEmail = new InternetAddress[RECIPIENTS.length];
				for (int i = 0; i < RECIPIENTS.length; i++) {
					receptientsEmail[i] = new InternetAddress(RECIPIENTS[i]);
				}

				//多个收件人
				msg.setRecipients(MimeMessage.RecipientType.TO, receptientsEmail);

				//3邮件内容:主题、内容
				msg.setSubject(emailSubject);
				//msg.setContent("Hello, 我是debug!!!", );//纯文本
				msg.setContent(emailContent, EMAIL_CONTENT_TYPE);//发html格式的文本
				//发送动作
				Transport.send(msg);

				LOG.info("邮件发送成功--" + "emailSubject");
			} catch (Exception excetion) {
				LOG.error("邮件发送失败: " + excetion);

			}
		}
	}


}
